package org.xfy.pet.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.xfy.base.util.AjaxResult;
import org.xfy.base.util.DistanceUtils;
import org.xfy.base.util.Point;
import org.xfy.base.util.examine.TextCensor;
import org.xfy.org.domain.Shop;
import org.xfy.org.mapper.ShopMapper;
import org.xfy.pet.domain.SearchMasterMsg;
import org.xfy.pet.domain.SearchMasterMsgAuditLog;
import org.xfy.pet.service.ISearchMasterMsgAuditLogService;
import org.xfy.pet.service.ISearchMasterMsgService;
import org.xfy.base.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.xfy.sys.domain.User;
import org.xfy.sys.mapper.UserMapper;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private TextCensor textCensor;
    @Autowired
    private ISearchMasterMsgAuditLogService msgAuditLogService;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    @Transactional
    public AjaxResult publish(SearchMasterMsg searchMasterMsg) {
        User user = userMapper.findOneByLogininfo(searchMasterMsg.getUserId());
        searchMasterMsg.setUserId(user.getId());
        super.add(searchMasterMsg);
        //百度智能审核
        if(textCensor.TextCensor(searchMasterMsg.getName())){
            return getAjaxResult(searchMasterMsg,-1,"名字不合规，审核不通过！");
        }
        if(textCensor.TextCensor(searchMasterMsg.getTitle())){
            return getAjaxResult(searchMasterMsg,-1,"标题不合规，审核不通过！");
        }if(textCensor.TextCensor(searchMasterMsg.getCoatColor())){
            return getAjaxResult(searchMasterMsg,-1,"颜色不合规，审核不通过！");
        }
        //审核通过更改searchMasterMsg状态
        searchMasterMsg.setNote("审核通过！,待人工审核");
        searchMasterMsg.setState(0);
        //获取最近的店铺信息
        //获取到用户传入的地址
        Point point = DistanceUtils.getPoint(searchMasterMsg.getAddress());
        List<Shop> all = shopMapper.findAll();
        //拿到最近的店铺
        Shop nearestShop = DistanceUtils.getNearestShop(point, all);
        searchMasterMsg.setShopId(nearestShop.getId());
        super.update(searchMasterMsg);
        saveSearchMasterMsg(searchMasterMsg.getId(),0,"审核通过！,待人工审核");
        return AjaxResult.me();
    }

    private AjaxResult getAjaxResult(SearchMasterMsg searchMasterMsg,Integer state,String message) {
        searchMasterMsg.setState(state);
        searchMasterMsg.setNote(message);
        super.update(searchMasterMsg);
        saveSearchMasterMsg(searchMasterMsg.getId(),state,message);
        return AjaxResult.me().setSuccess(false).setMessage(message);
    }

    private void saveSearchMasterMsg(Long id,Integer state,String message) {
        SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
        searchMasterMsgAuditLog.setMsgId(id);
        searchMasterMsgAuditLog.setState(state);
        searchMasterMsgAuditLog.setNote(message);
        msgAuditLogService.add(searchMasterMsgAuditLog);
    }
}
